<template>
  <div>
    <div class="p-4">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-form inline>
            <el-form-item>
              <el-button
                type="success"
                icon="el-icon-plus"
                @click="deBounceWrap(handleAdd)"
                >新增</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <el-table :data="data" border>
          <el-table-column
            prop="user_code"
            label="主播Id"
            width="150px"
          ></el-table-column>
          <el-table-column prop="nickname" label="主播昵称"></el-table-column>
          <el-table-column prop="priority" label="优先级" width="180px">
          </el-table-column>
          <el-table-column prop="recom_num" label="推荐次数"> </el-table-column>
          <el-table-column
            prop="into_room_num"
            label="进房次数"
          ></el-table-column>
          <el-table-column prop="create_time" label="创建时间">
          </el-table-column>
          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button
                type="primary"
                plain
                size="mini"
                icon="el-icon-edit"
                @click="deBounceWrap(handleEdit, scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                plain
                size="mini"
                icon="el-icon-delete"
                @click="deBounceWrap(handleDel, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
      </el-card>
    </div>

    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style:normal;color:red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 新增编辑 -->
    <el-dialog
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="40%"
      @close="handleClear"
      :before-close="handleClose"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="80px">
        <el-form-item prop="user_id" label="主播Id" label-width="120px">
          <el-input
            placeholder="请输入主播Id"
            clearable
            v-model.trim="form.user_id"
            @change="handleBlur"
          ></el-input>
        </el-form-item>
        <el-form-item label="主播名称" label-width="120px" :disabled="true">
          <el-input
            placeholder="主播名称"
            v-model="value"
            clearable
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item prop="priority" label="优先级" label-width="120px">
          <el-input
            placeholder="请输入优先级"
            clearable
            type="number"
            v-model.trim="form.priority"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" plain @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  toGetList,
  searchRoom,
  addList,
  detailList,
  editList,
  delList
} from "@/api/recommendationManagement/recommendedAnchor";
export default {
  data() {
    return {
      value: undefined,
      user_id: undefined,
      data: [],
      listQuery: {
        page: 1,
        page_size: 10
      },
      total: 0,
      titleOptions: {
        add: "新增",
        edit: "编辑"
      },
      title: "add",
      rowData: {},
      form: {
        user_id: undefined,
        priority: undefined
      },
      rules: {
        user_id: [{ required: true, message: "请输入主播Id", trigger: "blur" }],
        priority: [{ required: true, message: "请输入优先级", trigger: "blur" }]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //列表
    getList() {
      toGetList(this.listQuery).then(res => {
        this.data = res.data.data;
        this.total = res.data.total;
      });
    },
    //根据房间ID获取房间名称
    handleBlur() {
      searchRoom(this.form.user_id).then(res => {
        this.value = res.data.nickname;
        this.user_id = res.data.user_id;
      });
    },
    //新增
    handleAdd() {
      this.title = "add";
      this.dialogVisible = true;
    },
    //编辑
    async handleEdit(row) {
      this.title = "edit";
      this.rowData.id = row.id;
      await detailList({ id: this.rowData.id }).then(res => {
        this.form.user_id = res.data.user_code;
        this.form.priority = res.data.priority;
        this.value = res.data.nickname;
      });

      searchRoom(this.form.user_id).then(res => {
        this.user_id = res.data.user_id;
      });

      this.dialogVisible = true;
    },
    async handleSumbit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === "add") {
            let data = {
              user_id: this.user_id,
              priority: this.form.priority
            };
            addList(data).then(res => {
              if (res.code === 200) {
                this.$message.success("提交成功");
                this.getList();
                this.dialogVisible = false;
              } else {
              }
            });
          }
          if (this.title === "edit") {
            let data = {
              id: this.rowData.id,
              user_id: this.user_id,
              priority: this.form.priority
            };
            editList(data).then(res => {
              if (res.code === 200) {
                this.$message.success("编辑成功");
                this.getList();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false;
    },
    handleClear() {
      this.$refs["form"].resetFields();
      this.form = {
        user_id: undefined,
        priority: undefined
      };
      this.value = undefined;
    },
    //删除
    handleDel(row) {
      this.dataIds = { id: `${row.id}` };
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      delList(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功"
          });
        }
      });
    }
  }
};
</script>
